package com.startup.yellocar.component;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;
import com.startup.yellocar.dto.SMSDTO;
import com.startup.yellocar.utils.Utils;


@Component
public class SMSProviderServiceFSG implements SMSProviderService{
 
	
	String url="http://www.FreeSMSGateway.com/api_send";
	String accessToken="bd1ac918cd1b6050335132c1b0e118f8";
	
	@Autowired
	private RestTemplate restTemplate;
	
	public void send(SMSDTO smsDTO) throws Exception
	{
		 // Prepare acceptable media type
		  List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
		  acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
		   
		  // Prepare header
		  HttpHeaders headers = new HttpHeaders();
		  headers.setAccept(acceptableMediaTypes);
		  HttpEntity<String> entity = new HttpEntity<String>(headers); 
		  
		  
		
		  Gson gson = new Gson();
		  
		  Map<String, String> params = new HashMap<String, String>();
		  params.put("access_token", accessToken);
		  params.put("sent_to", "post_contacts");
		  params.put("message", URLEncoder.encode(smsDTO.getContent(),"UTF-8"));
		  params.put("post_contacts", gson.toJson(Utils.splitToList(smsDTO.getTo(), ";")));
		  
		  
		  // Send the request as GET
		  ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, params);

	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	
	
}
